#ifndef _BLOCKBREAKER_COMM_TCPSOCKET_H_
#define _BLOCKBREAKER_COMM_TCPSOCKET_H_

#include <afxwin.h>

#include <Comm/Include/SyncQueue.h>

// ** Ethernet MTU = 1500 bytes
// ** IP Header = 20 bytes
// ** TCP Header = 20 bytes
// ** Useful Size = 1460 = 1500 - 20 - 20 

#define TCP_MSG_SIZE 1460

typedef enum
{
    COMMUNICATION_LOST,
    WRITING_CLOSED_BY_OTHER_END,
    READING_CLOSED_BY_OTHER_END,
    READING_CLOSED
}
IPCResource_Exceptions;


class TCPSocket
{
    public:
        TCPSocket(SOCKET s, SyncQueue* TXQueue, SyncQueue* RxQueue, Semaphore* _sem );

        virtual ~TCPSocket(void);

        void write(SimpleMessage* msg);

        SimpleMessage* read();

        void terminateWriting();

        void terminateReading();

        void terminate();

    private:
        SOCKET s;

        Semaphore* sem_sync_owner;

        SyncQueue* TxQueue;

        SyncQueue* RxQueue;

        CWinThread* receiver_thread;

        CWinThread* sender_thread;

        static unsigned int SenderThreadFunction(void* params);

        static unsigned int ReceiverThreadFunction(void* params);

        void IniciarThreads();
};
typedef struct
{
    TCPSocket* ipc;
    SyncQueue* queue;
    Semaphore* sem;
}
IPC_Thread_Data;
#endif // _BLOCKBREAKER_COMM_TCPSOCKET_H_